package com.ffyc.resale.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ffyc.resale.model.GoodType;
import com.ffyc.resale.model.Goods;
import com.ffyc.resale.model.GoodsSearch;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Service;

import java.util.List;

public interface GoodsDao extends BaseMapper<Goods> {
    @Select("select * from goods left join user on goods.userid=user.id " +
            "left join good_type gt on goods.typeid=gt.id")
    @Results(
            {
                    @Result(property = "id", column = "id"),
                    @Result(property = "user", column = "userid",
                            one = @One(select = "com.ffyc.resale.dao.UserDao.selectById")),
                    @Result(property = "goodType", column = "typeid",
                            one = @One(select = "com.ffyc.resale.dao.GoodTypeDao.selectById"))
            }
    )
    IPage<Goods> list(Page<?> page);

    @Select("SELECT * FROM goods LEFT JOIN user ON goods.userid = user.id LEFT JOIN good_type gt ON goods.typeid = gt.id " +
            "WHERE goods.name LIKE CONCAT('%', #{name}, '%')")
    @Results(
            {
                    @Result(property = "id", column = "id"),
                    @Result(property = "user", column = "userid",
                            one = @One(select = "com.ffyc.resale.dao.UserDao.selectById")),
                    @Result(property = "goodType", column = "typeid",
                            one = @One(select = "com.ffyc.resale.dao.GoodTypeDao.selectById"))
            }
    )
    IPage<Goods> searchByName(Page<?> page, @Param("name") String name);

    @Select("SELECT * FROM goods LEFT JOIN user ON goods.userid = user.id LEFT JOIN good_type gt ON goods.typeid = gt.id " +
            "WHERE goods.typeid =#{typesid}")
    @Results(
            {
                    @Result(property = "id", column = "id"),
                    @Result(property = "user", column = "userid",
                            one = @One(select = "com.ffyc.resale.dao.UserDao.selectById")),
                    @Result(property = "goodType", column = "typeid",
                            one = @One(select = "com.ffyc.resale.dao.GoodTypeDao.selectById"))
            }
    )
    IPage<Goods> searchByType1(Page<?> page, @Param("typesid") Integer typesid);

    @Select("SELECT * FROM goods LEFT JOIN user ON goods.userid = user.id LEFT JOIN good_type gt ON goods.typeid = gt.id " +
            "WHERE goods.typesid =#{typesid}")
    @Results(
            {
                    @Result(property = "id", column = "id"),
                    @Result(property = "user", column = "userid",
                            one = @One(select = "com.ffyc.resale.dao.UserDao.selectById")),
                    @Result(property = "goodType", column = "typeid",
                            one = @One(select = "com.ffyc.resale.dao.GoodTypeDao.selectById"))
            }
    )
    IPage<Goods> searchByType2(Page<?> page, @Param("typesid") Integer typesid);

    @Select("SELECT * FROM goods LEFT JOIN user ON goods.userid = user.id LEFT JOIN good_type gt ON goods.typeid = gt.id " +
            "WHERE goods.typeid =#{typesid} and goods.status=#{status}")
    @Results(
            {
                    @Result(property = "id", column = "id"),
                    @Result(property = "user", column = "userid",
                            one = @One(select = "com.ffyc.resale.dao.UserDao.selectById")),
                    @Result(property = "goodType", column = "typeid",
                            one = @One(select = "com.ffyc.resale.dao.GoodTypeDao.selectById"))
            }
    )
    IPage<Goods> searchByType1AndStatus(Page<Goods> page, @Param("typesid") Integer typesid, @Param("status") Integer status);

    @Select("SELECT * FROM goods LEFT JOIN user ON goods.userid = user.id LEFT JOIN good_type gt ON goods.typeid = gt.id " +
            "WHERE goods.typesid =#{typesid} and goods.status=#{status}")
    @Results(
            {
                    @Result(property = "id", column = "id"),
                    @Result(property = "user", column = "userid",
                            one = @One(select = "com.ffyc.resale.dao.UserDao.selectById")),
                    @Result(property = "goodType", column = "typeid",
                            one = @One(select = "com.ffyc.resale.dao.GoodTypeDao.selectById"))
            }
    )
    IPage<Goods> searchByType2AndStatus(Page<Goods> page, @Param("typesid") Integer typeId, @Param("status") Integer status);

    @Select("SELECT * FROM goods LEFT JOIN user ON goods.userid = user.id LEFT JOIN good_type gt ON goods.typeid = gt.id " +
            "WHERE  goods.status=#{status}")
    @Results(
            {
                    @Result(property = "id", column = "id"),
                    @Result(property = "user", column = "userid",
                            one = @One(select = "com.ffyc.resale.dao.UserDao.selectById")),
                    @Result(property = "goodType", column = "typeid",
                            one = @One(select = "com.ffyc.resale.dao.GoodTypeDao.selectById"))
            }
    )
    IPage<Goods> searchByStatus(Page<Goods> page, @Param("status")Integer status);

    @Select("SELECT * FROM goods LEFT JOIN user ON goods.userid = user.id LEFT JOIN good_type gt ON goods.typeid = gt.id " +
            "WHERE goods.userid = #{userid}")
    @Results(
            {
                    @Result(property = "id", column = "id"),
                    @Result(property = "user", column = "userid",
                            one = @One(select = "com.ffyc.resale.dao.UserDao.selectById")),
                    @Result(property = "goodType", column = "typeid",
                            one = @One(select = "com.ffyc.resale.dao.GoodTypeDao.selectById"))
            }
    )
    Page<Goods> selectByUserId(Page<?> page, Integer userid);
    @Select("SELECT * FROM goods LEFT JOIN user ON goods.userid = user.id LEFT JOIN good_type gt ON goods.typeid = gt.id " +
            "WHERE goods.userid = #{userid} and goods.status = #{status}")
    @Results(
            {
                    @Result(property = "id", column = "id"),
                    @Result(property = "user", column = "userid",
                            one = @One(select = "com.ffyc.resale.dao.UserDao.selectById")),
                    @Result(property = "goodType", column = "typeid",
                            one = @One(select = "com.ffyc.resale.dao.GoodTypeDao.selectById"))
            }
    )
    Page<Goods> selectByUserIdAndStatus(Page<Goods> page, Integer userid, Integer status);
    IPage<Goods> findGoods(@Param("goodsIPage") IPage<Goods> page);

    @Select("SELECT id,image,NAME FROM goods\n" +
            "WHERE num > 0 AND STATUS = 1\n" +
            "ORDER BY TIME\n" +
            "LIMIT 0,4")
    List<Goods> selectNewGoods();

}
